package com.medusa.aps.business.modules.modeling.model.input;

import cn.hutool.core.util.IdUtil;
import com.medusa.aps.business.model.enums.ProductionType;
import com.medusa.aps.business.modules.modeling.mp.entity.BomItem;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.Valid;
import jakarta.validation.constraints.DecimalMax;
import jakarta.validation.constraints.DecimalMin;
import jakarta.validation.constraints.Digits;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
import lombok.experimental.Accessors;

import java.math.BigDecimal;
import java.util.List;

import static com.medusa.aps.business.model.constant.DigitConstants.MYSQL_DECIMAL_10_2;
import static com.medusa.aps.business.model.constant.DigitConstants.ZERO_TWELVE_DIGIT_NUMBER_PROMPTS;

/**
 * @author miskw
 * @date 2023/10/9
 */
@Data
@Accessors(chain = true)
@Schema(description = "BOM子项物料")
public class BomItemDTO {
    /**
     * 层级
     */
    @Schema(description = "层级")
    @NotNull(message = "层级不能为空")
    private Integer level;
    /**
     * 物料id
     */
    @Schema(description = "物料id")
    @NotNull(message = "物料id不能为空")
    private Long materialId;
    /**
     * 物料编码
     */
    @Schema(description = "物料编码")
    @NotNull(message = "物料编码不能为空")
    private String materialCode;
    /**
     * 物料名称
     */
    @Schema(description = "物料名称")
    @NotNull(message = "物料名称不能为空")
    private String materialName;
    /**
     * 物料规格
     */
    @Schema(description = "物料规格")
    private String materialSpecification;

    /**
     * 生产类型
     */
    @Schema(description = "生产类型")
    private ProductionType productionType;

    /**
     * 单位
     */
    @Schema(description = "单位")
    private String unit;

    /**
     * 单件用量
     */
    @Schema(description = "单件用量")
    @NotNull(message = "单件用量不能为空")
    @Digits(integer = 10, fraction = 2, message = "单件用量最多保留两位小数")
    @DecimalMin(value = "0.01", message = "单件用量最小为0.01")
    @DecimalMax(value = MYSQL_DECIMAL_10_2, message = "单件用量" + ZERO_TWELVE_DIGIT_NUMBER_PROMPTS)
    private BigDecimal unitUsage;

    /**
     * 备注
     */
    @Schema(description = "备注")
    private String remarks;

    /**
     * 子项物料
     */
    @Valid
    @Schema(description = "子项物料")
    private List<BomItemDTO> children;

    public BomItem toBeanBomItem(Integer bomId, String bomNo) {
        return toBeanBomItem(bomId, bomNo, null);
    }

    public BomItem toBeanBomItem(Integer bomId, String bomCode, Long id) {
        return new BomItem()
                .setId(id == null ? IdUtil.getSnowflakeNextId() : id)
                .setLevel(level)
                .setBomId(bomId)
                .setBomCode(bomCode)
                .setMaterialId(materialId)
                .setMaterialCode(materialCode)
                .setMaterialName(materialName)
                .setUnit(unit)
                .setUnitUsage(unitUsage)
                .setMaterialSpecification(materialSpecification)
                .setProductionType(productionType)
                .setRemarks(remarks);
    }
}
